package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroup;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    //根据分组id分组信息
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        //构建条件查询器

        if (!StringUtils.isNullOrEmpty(key)) {
            System.out.println(key);
            wrapper.and((obj) -> {
                obj.eq("attr_group_id ", key).or().like("attr_group_name", key);
            });
        }
            //查询所有
            if (categoryId == 0) {
                IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
                return new PageUtils(page);

            } else {

                IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
                return new PageUtils(page);
            }

        }

        @Autowired
        private AttrService attrService;
        //根据分类id,查找所有关联的分组，分组里查找所有的属性
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> id = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));


        List<AttrGroupWithAttrsVo> collect = id.stream().map(item -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());

            attrGroupWithAttrsVo.setAttrs(relationAttr);
            return attrGroupWithAttrsVo;

        }).collect(Collectors.toList());
        return collect;
    }

    //根据spuId,查询所有分组以及分组下的所有属性和属性值
    @Override
    public List<SpuItemAttrGroup> getAttrGroupWithAttrsBySpuId(Long spuId,Long catalogId) {
        List<SpuItemAttrGroup>  spuItemAttrGroups= this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);


        return spuItemAttrGroups;
    }


}